<?php

class Product extends CI_Model
{

    var $id = '';
    var $name = '';
    var $content = '';
    var $censored = 0;
    var $category_id = '';
    var $price = 0;
    var $posted_time = 0;
    var $user_id = null;
    protected $_table = 'product';

    function __construct()
    {
        parent::__construct();
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('Product', 10);
        return $query->result();
    }

    function get_pending($offset, $start)
    {
        $this->db->select('name, url, price, product.id as id');
        $this->db->where("censored", "0");
        $this->db->where("thumbnail", "1");
        $this->db->from('product');
        $this->db->join('image', 'image.product_id = product.id');
        $this->db->limit($offset, $start);
        $this->db->order_by("posted_time", "desc");
        $query = $this->db->get();
        return $query->result();
    }

    function get_all_pending()
    {
        $this->db->select('product.id as id, product.name as name, user.name as user_name, posted_time , price');
        $this->db->where("censored", "0");
        $this->db->from('product');
        $this->db->join('user', 'user.id = product.user_id');
        $this->db->order_by("posted_time", "desc");
        $query = $this->db->get();
        return $query->result();
    }

    function count_pending()
    {
        $this->db->where("censored", "0");
        $this->db->where("thumbnail", "1");
        $this->db->from('product');
        $this->db->join('image', 'image.product_id = product.id');
        return $this->db->count_all_results();
    }

    function get_official($offset, $start)
    {
        $this->db->select('name, url, price, product.id as id');
        $this->db->where("censored", "1");
        $this->db->where("thumbnail", "1");
        $this->db->from('product');
        $this->db->join('image', 'image.product_id = product.id');
        $this->db->limit($offset, $start);
        $this->db->order_by("posted_time", "desc");
        $query = $this->db->get();
        return $query->result();
    }

    function count_official()
    {
        $this->db->where("censored", "1");
        $this->db->where("thumbnail", "1");
        $this->db->from('product');
        $this->db->join('image', 'image.product_id = product.id');
        return $this->db->count_all_results();
    }

    public function get_all($offset, $start)
    {
        $this->db->select('name, url, price, product.id as id');
        $this->db->where("thumbnail", "1");
        $this->db->from('product');
        $this->db->join('image', 'image.product_id = product.id');
        $this->db->limit($offset, $start);
        $this->db->order_by("posted_time", "desc");
        $query = $this->db->get();
        return $query->result();
    }

    public function get_all_product($sort = 1, $cat = 0, $uid = 0, $query = '', $tag = 0) //Admin page
    {
        $this->db->select('product.id as id,(select count(*) from purchasing where purchasing.product_id = product.id) as count, product.name as name, content, censored, user.name as user_name,
            category.name as category_name,category.id as cat_id, posted_time , price, user.id as uid');
        $this->db->where("censored", "1");
        if ($cat != 0) $this->db->where("category_id", $cat);
        if ($uid != 0) $this->db->where("user.id", $uid);
        if ($query != '') $this->db->where("(product.name like '%" . $query . "%' or category.name like '%" . $query . "' or user.name like '%" . $query . "%')");
        if ($sort == 1) $this->db->order_by('posted_time', 'desc');
        if ($sort == 2) $this->db->order_by('count', 'desc');
        if ($sort == 3) $this->db->order_by('price', 'desc');
        if ($sort == 4) $this->db->order_by('price', 'asc');
        if ($tag != 0) {
            $this->db->join('tagging', 'tagging.product_id = product.id');
            $this->db->where('tag_id', $tag);
        }
        $this->db->from('product');
        $this->db->join('category', 'category.id = product.category_id');
        $this->db->join('user', 'user.id = product.user_id');
        $this->db->where('censored', 1);
        $query = $this->db->get();
        return $query->result();
    }

    public function count_all()
    {
        return $this->db->count_all($this->_table);
    }

    public function get($id)
    {
        $this->db->select('product.id as id, product.name as name, content, censored, user.name as user_name, user.id as uid,
            category.name as category_name, category.id as cid, posted_time , price');
        $this->db->where("product.id", $id);
        $this->db->from('product');
        $this->db->join('category', 'category.id = product.category_id');
        $this->db->join('user', 'user.id = product.user_id');
        $query = $this->db->get();
        return $query->result();
    }

    public function get_tags($product_id)
    {
        $this->db->where("product_id", $product_id);
        $this->db->from("tagging");
        $this->db->join('tag', 'tagging.tag_id = tag.id');
        $query = $this->db->get();
        return $query->result();
    }

    public function get_images($product_id)
    {
        $this->db->where("product_id", $product_id);
        $this->db->from("image");
        $query = $this->db->get();
        return $query->result();
    }

    function save()
    {
        $session = $this->session->userdata('logged_in');
        $this->user_id = $session['id'];

        $this->name = $this->input->post('name');
        $this->category_id = $this->input->post('cid');
        $this->content = $this->input->post('content');
        $this->price = $this->input->post('price');
        $this->posted_time = time();

        $this->db->insert('product', $this);

        $product_id = $this->db->insert_id();

        $tags = explode(',', $this->input->post('tags'));
        foreach ($tags as $tag) {
            if ($tag != '') {
                $this->db->select('id');
                $this->db->from('tag');
                $this->db->where('name', $tag);
                $result = $this->db->get()->result();
                if (count($result) == 0) {
                    $this->db->insert('tag', array('name' => $tag));
                    $this->db->select('id');
                    $this->db->from('tag');
                    $this->db->where('name', $tag);
                    $result = $this->db->get()->result();
                }
                $tag_id = $result[0]->id;
                $this->db->insert('tagging', array('tag_id' => $tag_id, 'product_id' => $product_id));
            }
        }

        return $product_id;

    }

    function update()
    {
        $name = $this->input->post('name');
        $category_id = $this->input->post('cid');
        $content = $this->input->post('content');
        $price = $this->input->post('price');
        $id = $this->input->post('id');

        $this->db->update('product', array(
            'name' => $name,
            'category_id' => $category_id,
            'content' => $content,
            'price' => $price,
            'censored' => 0
        ), array("id" => $id));

        $tags = explode(',', $this->input->post('tags'));

        $this->db->query("delete from tagging where product_id = $id");

        foreach ($tags as $tag) {
            if ($tag != '') {
                $this->db->select('id');
                $this->db->from('tag');
                $this->db->where('name', $tag);
                $result = $this->db->get()->result();
                if (count($result) == 0) {
                    $this->db->insert('tag', array('name' => $tag));
                    $this->db->select('id');
                    $this->db->from('tag');
                    $this->db->where('name', $tag);
                    $result = $this->db->get()->result();
                }
                $tag_id = $result[0]->id;
                $this->db->insert('tagging', array('tag_id' => $tag_id, 'product_id' => $id));
            }
        }

        return 1;
    }

    function update_censored($id, $data)
    {
        try {
            $this->db->where("id", $id);
            $this->db->update('product', $data);
        } catch (Exception $e) {
            return 0;
        }
        return 1;
    }

    function get_purchased($sort = 1, $cat = 0, $uid = 0, $query = '', $tag = 0, $puid)
    {
        $this->db->select('product.id as id,(select count(*) from purchasing where purchasing.product_id = product.id) as count, product.name as name, content, censored, user.name as user_name,
            category.name as category_name,category.id as cat_id, posted_time , price, user.id as uid');
        if ($cat != 0) $this->db->where("category_id", $cat);
        if ($uid != 0) $this->db->where("user.id", $uid);
        if ($query != '') $this->db->where("(product.name like '%" . $query . "%' or category.name like '%" . $query . "' or user.name like '%" . $query . "%')");
        if ($sort == 1) $this->db->order_by('posted_time', 'desc');
        if ($sort == 2) $this->db->order_by('count', 'desc');
        if ($sort == 3) $this->db->order_by('price', 'desc');
        if ($sort == 4) $this->db->order_by('price', 'asc');
        if ($tag != 0) {
            $this->db->join('tagging', 'tagging.product_id = product.id');
            $this->db->where('tag_id', $tag);
        }
        $this->db->from('product');
        $this->db->join('category', 'category.id = product.category_id');
        $this->db->join('user', 'user.id = product.user_id');
        $this->db->where('censored', 1);
        $this->db->join('purchasing', 'purchasing.product_id = product.id');
        $this->db->where('purchasing.user_id', $puid);
        $this->db->where('purchasing.purchasing_time >', '0');
        $query = $this->db->get();
        return $query->result();
    }

    function get_uploaded_pending($sort = 1, $cat = 0, $uid = 0, $query = '', $tag = 0)
    {
        $this->db->select('product.id as id,(select count(*) from purchasing where purchasing.product_id = product.id) as count, product.name as name, content, censored, user.name as user_name,
            category.name as category_name,category.id as cat_id, posted_time , price, user.id as uid');
        if ($cat != 0) $this->db->where("category_id", $cat);
        if ($uid != 0) $this->db->where("user.id", $uid);
        if ($query != '') $this->db->where("(product.name like '%" . $query . "%' or category.name like '%" . $query . "' or user.name like '%" . $query . "%')");
        if ($sort == 1) $this->db->order_by('posted_time', 'desc');
        if ($sort == 2) $this->db->order_by('count', 'desc');
        if ($sort == 3) $this->db->order_by('price', 'desc');
        if ($sort == 4) $this->db->order_by('price', 'asc');
        if ($tag != 0) {
            $this->db->join('tagging', 'tagging.product_id = product.id');
            $this->db->where('tag_id', $tag);
        }
        $this->db->from('product');
        $this->db->join('category', 'category.id = product.category_id');
        $this->db->join('user', 'user.id = product.user_id');
        $this->db->where('censored', 0);
        $query = $this->db->get();
        return $query->result();
    }

    function hasPurchased($uid)
    {
        $query = $this->db->query("select * from purchasing where purchasing_time > 0 and user_id = $uid");
        if (count($query->result()) > 0) return true;
        return false;
    }

    function hasCensored($uid)
    {
        $query = $this->db->query("select * from product where user_id = $uid and censored = 1");
        if (count($query->result()) > 0) return true;
        return false;
    }

    function hasPending($uid)
    {
        $query = $this->db->query("select * from product where user_id = $uid and censored = 0");
        if (count($query->result()) > 0) return true;
        return false;
    }

    function get_promotion($pid)
    {
        $query = $this->db->query("select * from promotion where product_id = $pid");
        $result = $query->result();
        if (count($result) > 0) return $result[0];
        return false;
    }

    function create_promotion($pid, $start, $end, $discount)
    {
        $this->db->query("delete from promotion where product_id = $pid");
        $this->db->query("insert into promotion(product_id,start,end,discount) values($pid,$start,$end,$discount)");
        return 1;
    }

    function delete_promotion($pid)
    {
        $this->db->query("delete from promotion where product_id = $pid");
        return 1;
    }

    function count_downloads($id, $month, $year)
    {
        $query = $this->db->query("select count(*)
                        from purchasing, product
                        where purchasing.product_id = product.id and product.id = $id and purchasing.purchasing_time is not null
                        and month(FROM_UNIXTIME(purchasing_time)) = $month and year(FROM_UNIXTIME(purchasing_time)) = $year");
        return $query->result_array()[0];
    }
}